import json
import matplotlib.pyplot as plt
from collections import Counter

# 定义分段区间
bins = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

# 读取 JSON 文件
with open('data/iu_xray/iu_xray/generated_reports.json', 'r') as f:
    data = json.load(f)

# 提取 score 值
scores = [item['score'] for item in data]

# 统计各个区间的数量
counter = Counter()
for score in scores:
    for i in range(len(bins) - 1):
        if bins[i] <= score < bins[i + 1]:
            counter[(bins[i], bins[i + 1])] += 1
            break

# 提取区间和数量
intervals = list(counter.keys())
counts = list(counter.values())

# 绘制柱状图
plt.figure(figsize=(10, 6))
plt.bar(range(len(intervals)), counts, tick_label=[f'{i[0]:.1f}-{i[1]:.1f}' for i in intervals])
plt.xlabel('Score Interval')
plt.ylabel('Count')
plt.title('Distribution of Scores in Generated Reports')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()